﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System .Data ;
using System .Data .SqlClient ;
using dhDataAcess.orderLinqDAL ;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;

namespace dhDataAcess.orderDAL
{
  public   class materialListDAL
    {
        string tableName = "materialList";
        private SqlTransaction tran = null;
        private SqlConnection conn = null;
        public materialListDAL(SqlTransaction _transaction, SqlConnection _conn)
        {
            tran = _transaction ;
            conn = _conn;
        }
        /// <summary>
        /// 插入原材料产品表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int materialListInsert(materialListModel row)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string insertstr = baseBLL.dTableColumnRead.insertSqlStr(tableName).ToString();
            SqlParameter[] para = {
                                    new SqlParameter ("@mark",row .mark ),
                                    new SqlParameter ("@updateUser",row .updateUser),
                                    new SqlParameter ("@updateDate",row .updateDate),
                                    new SqlParameter ("@createUser",row .createUser),
                                    new SqlParameter ("@createDate",row .createDate),
                                    new SqlParameter ("@posName",row .posName ),
                                    new SqlParameter ("@stockName",row .stockName ),
                                    new SqlParameter ("@matLot",row .matLot),
                                    new SqlParameter ("@Fzunit",row .Fzunit ),
                                    new SqlParameter ("@matpord",row .matpord  ),
                                      new SqlParameter ("@supplier",row .supplier  ),
                                    new SqlParameter ("@unit",row .unit ),
                                    new SqlParameter ("@matSpec",row .matSpec ),
                                    new SqlParameter ("@matName",row .matName ),
                                    new SqlParameter ("@mtype",row .mtype ),
                                    new SqlParameter ("@matNum",row .matNum),
                                     new SqlParameter ("@stockQty","0" ),
                                      new SqlParameter ("@fqty","0" ),
                                      new SqlParameter ("@price",row.price  ),
                                      new SqlParameter ("@stockQtyqc","0" )
                                };
            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertstr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 导入原材料产品表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int materialListimportInsert(orderDataSet.materialListDataTable materialTable)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter par = new SqlParameter("@materialList", materialTable);
            par.SqlDbType = SqlDbType.Structured;
            par.TypeName = "materialList";


            cmd.Parameters.Add(par);
            cmd.CommandText = "materialListInsert";

            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 更新原材料产品表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int materialListUpdate(materialListModel row)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
          //  string updatestr = baseBLL.dTableColumnRead.updateSqlStr(tableName).ToString();
            string updatestr = "update materialList set mark=@mark,updateUser=@updateUser,updateDate=@updateDate,createUser=@createUser,createDate=@createDate,posName=@posName,stockName=@stockName,matLot=@matLot,Fzunit=@Fzunit,unit=@unit,matpord=@matpord,supplier=@supplier,matSpec=@matSpec,matName=@matName,mtype=@mtype,matNum=@matNum,price=@price where matNum=@matNum";
            SqlParameter[] para = {
                                    new SqlParameter ("@mark",row .mark ),
                                    new SqlParameter ("@updateUser",row .updateUser),
                                    new SqlParameter ("@updateDate",row .updateDate),
                                    new SqlParameter ("@createUser",row .createUser),
                                    new SqlParameter ("@createDate",row .createDate),
                                    new SqlParameter ("@posName",row .posName ),
                                    new SqlParameter ("@stockName",row .stockName ),
                                    new SqlParameter ("@matLot",row .matLot),
                                    new SqlParameter ("@Fzunit",row .Fzunit ),
                                    new SqlParameter ("@matpord",row .matpord  ),
                                     new SqlParameter ("@supplier",row .supplier  ),
                                    new SqlParameter ("@unit",row .unit ),
                                    new SqlParameter ("@matSpec",row .matSpec ),
                                    new SqlParameter ("@matName",row .matName ),
                                    new SqlParameter ("@mtype",row .mtype ),
                                    new SqlParameter ("@matNum",row .matNum),
                                     new SqlParameter ("@price",row.price  )
                                };
            cmd.Parameters.AddRange(para);
            cmd.CommandText = updatestr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 删除一行原材料产品表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
        public int materialListDel(string matNum)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string deletestr = baseBLL.dTableColumnRead.delSqlStr (tableName).ToString();
            SqlParameter[] para = {
                                   new SqlParameter ("@matNum",matNum)
                                };
            cmd.Parameters.AddRange(para);
            cmd.CommandText = deletestr ;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }

    }
}
